<?php
/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

/**
 * Handles configuration data stored in JSON files.
 * 
 * \code
 * Config::$chroot = APP.'config'.DS;
 * Config::load('blog', 'plugins'.DS.'blog.json');
 * 
 * # read:
 * $base_url = Config::get('blog', 'base_url');
 * 
 * # write:
 * Config::set('blog', $_POST['Blog']);
 * Config::set('blog', 'base_url', $_POST['Blog']['base_url']);
 * 
 * # save to APP/config/plugins/blog.json:
 * Config::save('blog');
 * \endcode
 */
class Config
{
	static public    $root;
	static public    $default_file = 'config.json';
	static protected $data         = array();
	static protected $files        = array();
	
	/**
	 * Loads config from files (in JSON format).
	 * 
	 * @param $file Mixed
	 * @param $trigger_notice Boolean
	 */
	static function load($file, $trigger_notice=true)
	{
		$file = (strpos($file, '/') === 0) ? $file : self::$root.$file;
		
		if (file_exists($file))
		{
			$data = json_decode(file_get_contents($file), true);
			foreach($data as $container => $d)
			{
				self::$data[$container]  =& $d;
				self::$files[$container] = $file;
			}
		}
#		else {
#			trigger_error("Config file not found: ".self::$root.$file, E_USER_NOTICE);
#		}
	}
	
	/**
	 * Loads config from files (in JSON format).
	 * 
	 * @param $file Mixed
	 */
	/*
	static function append($file)
	{
		if (file_exists(self::$root.$file))
		{
			$data = json_decode(file_get_contents(self::$root.$file), true);
			foreach($data as $container => $d)
			{
				foreach($d as $k => $v) {
					self::$data[$container][$k] = $v;
				}
			}
		}
	}
	*/
	
	/**
	 * Returns a config variable or a full container.
	 * 
	 * @return Mixed
	 * @param $container String
	 * @param $var String[optional]
	 */
	static function get($container, $var=null)
	{
		if ($var !== null)
		{
			return isset(self::$data[$container][$var]) ?
				self::$data[$container][$var] : null;
		}
		else
		{
			return isset(self::$data[$container]) ?
				self::$data[$container] : null;
		}
		
	}
	
	/**
	 * Checks wether a container/variable is set or not.
	 * 
	 * @return Boolean
	 * @param $container String
	 * @param $var String[optional]
	 */
	static function is_set($container, $var=null)
	{
		return isset(self::$data[$container], self::$data[$container][$var]);
	}
	
	/**
	 * Checks wether a container/variable is empty or not.
	 * 
	 * @return Boolean
	 * @param $container String
	 * @param $var String[optional]
	 */
	static function is_empty($container, $var=null)
	{
		return (empty(self::$data[$container]) or empty(self::$data[$container][$var]));
	}
	
	/**
	 * Sets a variable in a container.
	 * 
	 * @param $container String
	 * @param $data_or_var Mixed
	 * @param $value Mixed[Optional]
	 */
	static function set($container, $data_or_var, $value=null)
	{
		if (is_array($data_or_var))
		{
			foreach($data_or_var as $k => $v) {
				self::$data[$container][$k] = $v;
			}
		}
		else {
			self::$data[$container][$data_or_var] = $value;
		}
	}
	
	/**
	 * Saves config as JSON in a file.
	 * 
	 * @return Boolean
	 * @param $container Mixed
	 * @param $file String[Optional]
	 */
	static function save($container, $file=null)
	{
		if (empty($file))
		{
			$file = isset(self::$files[$container]) ?
				self::$files[$container] :
				self::$root.self::$default_file;
		}
		$data = array($container => self::$data[$container]);
		return file_put_contents($file, json_encode($data));
	}
}

class Config2component
{
	/**
	 * Loads default configuration on startup.
	 * 
	 * @param $Controller Object
	 */
	function startup(Controller $Controller)
	{
		if (empty(Config::$root)) {
			Config::$root = APP.'config'.DS;
		}
#		if (Core::config_autoload)
#		{
			# website
			Config::load('config.json', false);
			
			# plugin
			if (!empty(Core::$plugin)) {
				Config::load('plugins'.DS.Core::$plugin.'.json', false);
			}
#		}
	}
}
?>